﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;

/// <summary>
/// DataBaseAccess の概要の説明です
/// </summary>
public class DataBaseAccessGroup
{
    private static string GetConnectionString()
    {
        ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"];
        return setting.ConnectionString;
    }
    public static bool IsAnonymousGroup(string group)
    {
        SqlConnection objDb = new SqlConnection(GetConnectionString());
        SqlCommand objCom = new SqlCommand("SELECT count(*) FROM anonymous WHERE UserName=@group", objDb);
        objCom.Parameters.AddWithValue("@group", Sanitizing.SafeSQLLiteral(group));

        int cnt = 0;
        try
        {
            objDb.Open();
            cnt = (int)objCom.ExecuteScalar();
        }
        finally
        {
            objDb.Close();
        }

        if (cnt == 1) return true;
        return false;
    }

    public static bool RegisterAnonymousGroup(string group)
    {
        SqlConnection objDb = new SqlConnection(GetConnectionString());
        SqlCommand objCom = new SqlCommand("INSERT INTO anonymous (UserName) VALUES (@group)", objDb);
        objCom.Parameters.AddWithValue("@group", Sanitizing.SafeSQLLiteral(group));
        int affectedRows = 0;
        try
        {
            objDb.Open();
            affectedRows = objCom.ExecuteNonQuery();
        }
        finally
        {
            objDb.Close();
        }

        if (affectedRows != 1) return false;

        return true;
    }
}
